﻿using System;
using System.Collections.Generic;
using System.Collections;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.Default.SERVER.role
{
    public partial class MenuTreeAll : System.Web.UI.Page
    {
        string strTree = "";
        int roleId = 0;
        //新模块ID列表
        ArrayList menus = null;

        MsSqlDal<User_TreeMenu> iUtm = new MsSqlDal<User_TreeMenu>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Request.Params["roleid"] != null)
                    {
                        roleId = Convert.ToInt32(Request.Params["roleid"]);
                    }
                    strTree = "";
                    menus = new ArrayList();
                    menus = UserMenuList();
                    TreeData(0);
                    Response.Write(strTree);
                }
                catch { Response.Write("[]"); }
            }
        }

        /// <summary>
        /// 判断当前菜单是否是叶节点
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        private string IsLeaf(int ID)
        {
            IList<User_TreeMenu> list = iUtm.GetList("ParentID=" + ID, null, "", 0);

            if (list.Count > 0)
            {
                return "false";
            }

            return "true";
        }

        /// <summary>
        /// 递归树
        /// </summary>
        /// <param name="parentID"></param>
        private void TreeData(int parentID)
        {
            MsSqlDal<User_TreeMenu> iUtm = new MsSqlDal<User_TreeMenu>();

            IList<User_TreeMenu> list = iUtm.GetList("ParentID=" + parentID, null, "", 0);

            strTree += "[";

            foreach (User_TreeMenu utm in list)
            {
                strTree += "{'checked':" + (IsExists(menus, utm.ID) ? "true" : "false") + ",'id': '" + utm.ID + "','iconCls': '" + utm.IconCls + "','isClass':" + (utm.IsClass ? "true" : "false") + ",'text':'<b style=\"color:#006699\">" + utm.Text + "</b>','cls':'" + utm.Cls + "','leaf':false";

                if (!IsLeaf(utm.ID).Equals("true"))
                {
                    strTree += ",'children':";
                    TreeData(utm.ID);
                }
                else
                {
                    strTree += ",'children':[" +
                        "{'iconCls':'icon-add','checked':" + (IsPv(utm.ID, roleId, "A") ? "true" : "false") + ",'id':'" + utm.ID + "-A','text':'添加','leaf':true}," +
                        "{'iconCls':'icon-edit','checked':" + (IsPv(utm.ID, roleId, "U") ? "true" : "false") + ",'id':'" + utm.ID + "-U','text':'更新','leaf':true}," +
                        "{'iconCls':'icon-del','checked':" + (IsPv(utm.ID, roleId, "D") ? "true" : "false") + ",'id':'" + utm.ID + "-D','text':'删除','leaf':true}" +
                        "]";
                }
                strTree += "},";
            }
            strTree = strTree.TrimStart(',');
            strTree = strTree.TrimEnd(',');
            strTree += "]";
        }

        /// <summary>
        /// 获取用户菜单ID列表
        /// </summary>
        /// <returns></returns>
        public ArrayList UserMenuList()
        {
            //新模块ID列表
            ArrayList menuList = new ArrayList();
            MsSqlDal<User_Info> iUi = new MsSqlDal<User_Info>();
            MsSqlDal<User_RoleConfig> iUrc = new MsSqlDal<User_RoleConfig>();
            MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();

            //按角色ID查询模块列表
            IList<User_RolePermissionConfig> iUrpcList = iUrpc.GetList("RoleID=" + roleId, null, "", 0);

            //遍历模块列表
            foreach (User_RolePermissionConfig uprc in iUrpcList)
            {
                //如果新模块列表不存在当前ID
                if (!IsExists(menuList, uprc.MenuID))
                {
                    //向新模块列表插入当前ID
                    menuList.Add(uprc.MenuID);
                }
            }
            return menuList;
        }

        /// <summary>
        /// 判断ID是否存在与ArrayList中
        /// </summary>
        /// <param name="ArrayList">列表</param>
        /// <param name="ID">ID</param>
        /// <returns></returns>
        private bool IsExists(ArrayList list, int ID)
        {
            if (list == null) { return false; }
            foreach (int id in list)
            {
                if (id.Equals(ID))
                    return true;
            }
            return false;
        }

        private bool IsPv(int modelId, int roleId, string command)
        {
            bool validation = false;

            MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();

            User_RolePermissionConfig urpc = iUrpc.GetModel("RoleID=" + roleId + " AND MenuID=" + modelId, null);

            if (urpc == null) return false;

            if (urpc.PermissionValue != "" && urpc.PermissionValue.Length == 4)
            {
                validation = GetCP(command, urpc.PermissionValue);
            }

            return validation;
        }

        private bool GetCP(string command, string permissionValue)
        {
            bool validation = false;

            char[] pvs = permissionValue.ToCharArray();

            switch (command)
            {
                default://查
                    if (pvs[0] == '1') validation = true;
                    break;
                case "A"://增
                    if (pvs[1] == '1') validation = true;
                    break;
                case "U"://改
                    if (pvs[2] == '1') validation = true;
                    break;
                case "D"://删
                    if (pvs[3] == '1') validation = true;
                    break;
            }

            return validation;
        }
    }
}